<?php session_start();?>
<?php require_once('Connections/database.php'); ?>
<?php
if(!isset($_SESSION['user']))
{
	header("Location: login.php");	
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_SanPham = 20;
$pageNum_SanPham = 0;
if (isset($_GET['pageNum_SanPham'])) {
  $pageNum_SanPham = $_GET['pageNum_SanPham'];
}
$startRow_SanPham = $pageNum_SanPham * $maxRows_SanPham;

mysql_select_db($database_database, $database);
$query_SanPham = "SELECT * FROM sanpham WHERE Xoa = False ORDER BY idSanPham ASC";
$query_limit_SanPham = sprintf("%s LIMIT %d, %d", $query_SanPham, $startRow_SanPham, $maxRows_SanPham);
$SanPham = mysql_query($query_limit_SanPham, $database) or die(mysql_error());
$row_SanPham = mysql_fetch_assoc($SanPham);

if (isset($_GET['totalRows_SanPham'])) {
  $totalRows_SanPham = $_GET['totalRows_SanPham'];
} else {
  $all_SanPham = mysql_query($query_SanPham);
  $totalRows_SanPham = mysql_num_rows($all_SanPham);
}
$totalPages_SanPham = ceil($totalRows_SanPham/$maxRows_SanPham)-1;

$queryString_SanPham = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_SanPham") == false && 
        stristr($param, "totalRows_SanPham") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_SanPham = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_SanPham = sprintf("&totalRows_SanPham=%d%s", $totalRows_SanPham, $queryString_SanPham);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Admin</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link href="Content/style.css" rel="stylesheet" type="text/css" />
    <link href="Content/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.9.2.custom.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="Scripts/ckeditor/ckeditor.js" type="text/javascript"></script>
    <script src="Scripts/ckeditor/samples/sample.js"></script>
	<script type="text/javascript" src="Editor/scripts/innovaeditor.js"></script>
	
    <script type="text/javascript">
        $(document).ready(function () {
            $('.dropdown').hover(function () {
                $(this).find('.sub_navigation').toggle();
            });
        });

        function MenuClick(url) {
            window.location.href = url;
        }
    </script>
   <script type="text/javascript">
    $(document).ready(function () {
        $("#MenuSanPham").removeClass("dropdown").addClass("active");
    });
</script>
</head>
<body>
    <div id="wrap">
        <div id="header">
            <img alt="dat nuoc viet logo" src="Content/Images/gift_logo_20.png" height="100px"/>
        </div>
        <div id="menu">
            <ul id="navigation">
                <li class="dropdown" id="MenuHome"><a href="index.php" class="menuitem">Trang chủ</a></li>
                <li class="dropdown" id="MenuDanhMuc"><a href="DanhMuc.php" class="menuitem">Danh mục</a>
                </li>
                <li class="dropdown" id="MenuSanPham"><a href="SanPham.php" class="menuitem">Sản phẩm</a>
                </li>
                <li class="dropdown" id="MenuCP" onclick="MenuClick('/Admin/AdminHome/ChangePassword');"><a href="/Admin/AdminHome/ChangePassword" class="menuitem">Thay đổi mật khẩu</a></li>
            </ul>
        </div>

        <div id="date">
            <div class="datecontrol">
                <a href="../index.php" target="_blank">Trang chủ</a>&nbsp;&nbsp;&nbsp;
                <a href="logout.php">Đăng xuất</a>
            </div>
            <div class="clear">&nbsp;</div>
        </div>
      <div id="content">
			<div class="contenttitle">
                <div class="titleplace">
                    <span>Sản phẩm</span>&nbsp;&nbsp;&nbsp;
                </div>
                <div class="buttonplace">
                    <a href="SanPham_addnew.php">
                        <input type="button" value="Thêm mới sản phẩm" />
                    </a>
                </div>
                <div class="clear">&nbsp;</div>
            </div>
   		<div class="contentitem">
   		  <table border="0">
                  <tr>
                    <td><?php if ($pageNum_SanPham > 0) { // Show if not first page ?>
                        <a href="<?php printf("%s?pageNum_SanPham=%d%s", $currentPage, 0, $queryString_SanPham); ?>">Trang đầu</a>
                        <?php } // Show if not first page ?></td>
                    <td><?php if ($pageNum_SanPham > 0) { // Show if not first page ?>
                        <a href="<?php printf("%s?pageNum_SanPham=%d%s", $currentPage, max(0, $pageNum_SanPham - 1), $queryString_SanPham); ?>">Trang trước</a>
                        <?php } // Show if not first page ?></td>
                    <td><?php if ($pageNum_SanPham < $totalPages_SanPham) { // Show if not last page ?>
                        <a href="<?php printf("%s?pageNum_SanPham=%d%s", $currentPage, min($totalPages_SanPham, $pageNum_SanPham + 1), $queryString_SanPham); ?>">Trang sau</a>
                        <?php } // Show if not last page ?></td>
                    <td><?php if ($pageNum_SanPham < $totalPages_SanPham) { // Show if not last page ?>
                        <a href="<?php printf("%s?pageNum_SanPham=%d%s", $currentPage, $totalPages_SanPham, $queryString_SanPham); ?>">Trang Cuối</a>
                        <?php } // Show if not last page ?></td>
                  </tr>
            </table>
            
        <table class="tablecontent">
                    <thead>
                      <tr>
                        <th style="width:50px;">&nbsp;
                          
                          </th>
                          <th style="width:120px;text-align:center;">
                          	Hình ảnh
                          </th>
                          <th>Mã sản phẩm</th>
                        <th>
                          Tên sản phẩm
                          </th>
                        <th style="width:70px;text-align:center;">Sẩn phẩm mới</th>
                        <th style="width:70px;text-align:center;">Sẩn phẩm Hot</th>
                        <th style="width:120px;text-align:center;">
                          Giá
                          </th>
                        <th style="width:70px">&nbsp;</th>
                        </tr>            
                      </thead>
                    <tbody id="tablecontent">
                    <?php $i = 0 ;do { $i++; ?>
                    	
                      <tr class="<?php if($i%2===0) {echo 'event';} else {echo 'odd';}?>">
                        <td>
                          <?php echo $row_SanPham['idSanPham']; ?>
                          </td>
                          <td>
                          	<img src="../<?php echo $row_SanPham['urlHinh']; ?>" width="50px" height="50px" align="middle"/>
                          </td>
                          <td><?php echo $row_SanPham['MaSanPham']; ?></td>
                        <td>
                          <?php echo $row_SanPham['TenSP']; ?>
                          </td>
                          <td style="text-align:center;"><?php echo $row_SanPham['New']; ?></td>
                          <td style="text-align:center;"><?php echo $row_SanPham['Hot']; ?></td>
                        <td style="text-align:center;">
                          <?php echo $row_SanPham['Gia']; ?>
                          </td>
                        <td>
                          <a href="SanPham_edit.php?id=<?php echo $row_SanPham['idSanPham'];?>">Sửa</a> | 
                          <a href="SanPham_edit.php?xoa=<?php echo $row_SanPham['idSanPham'];?>">Xóa</a>
                          </td>
                        </tr>
                       <?php } while ($row_SanPham = mysql_fetch_assoc($SanPham)); ?>
                      </tbody>
                  </table>
                 
        </div>
      </div>      
    </div>  
</body>
</html>
<?php
mysql_free_result($SanPham);
?>
